<template>
  <div>
    <el-table v-loading="loading" :data="tableData" class="mb20" stripe border style="width: 100%">
      <el-table-column prop="id" label="Id" width="60" header-align="center" align="center" />
      <el-table-column prop="name" label="RoleName" header-align="center" align="center" />
      <el-table-column prop="img" label="RoleImg" header-align="center" align="center">
        <template #default="{ row }">
          <el-image :src="row.img" fit="fill" style="width: 100px; height: 100px"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作" :min-width="90" align="center">
        <template #default="scope">
          <el-popconfirm
            icon="el-icon-info"
            icon-color="red"
            title="确定删除吗？"
            placement="top"
            @confirm="handleDelete(scope.row.id)"
          >
            <template #reference>
              <el-button text type="primary" size="small" style="margin-left: 10px">
                {{ '删除' }}
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:page-size="pageSize"
      v-model:current-page="pageIndex"
      background
      layout="total, sizes, prev, pager, next"
      :total="total"
      :page-sizes="[10, 20, 30, 50]"
      @size-change="getData"
      @current-change="getData"
    />
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import { getList, deleteItem } from '@/api/role'

let loading = ref(false)
let pageIndex = ref(1)
let pageSize = ref(10)
let total = ref(0)
let tableData = ref([])

onMounted(() => {
  getData()
})

// 获取数据
const getData = () => {
  loading.value = true
  getList({ pageIndex: pageIndex.value, pageSize: pageSize.value }).then(res => {
    console.log('getData res', res)
    loading.value = false

    const { records, total: count } = res.data
    tableData.value = records || []
    total.value = count
  })
}

// 删除
const handleDelete = id => {
  deleteItem({ id }).then(res => {
    console.log('delete res', res)
    if (res.data) getData()
  })
}
</script>

<script>
import { defineComponent } from 'vue'
export default defineComponent({
  name: 'Role' // 指定组件名称
})
</script>

<style scoped>
.mb20 {
  margin-bottom: 20px;
}
</style>
